


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: StrategyPropertyAccessor</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.dormancy.access</a> ]
</div>

<h1>Coverage Summary for Class: StrategyPropertyAccessor (at.dormancy.access)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">StrategyPropertyAccessor</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (12/ 12)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (22/ 22)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2013 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.dormancy.access;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import org.springframework.beans.*;
<i>19</i>&nbsp;import org.springframework.core.MethodParameter;
<i>20</i>&nbsp;import org.springframework.core.convert.TypeDescriptor;
<i>21</i>&nbsp;
<i>22</i>&nbsp;import javax.annotation.Nonnull;
<i>23</i>&nbsp;import javax.annotation.Nullable;
<i>24</i>&nbsp;
<i>25</i>&nbsp;import static at.dormancy.access.AbstractPropertyAccessStrategy.AccessType.FIELD;
<i>26</i>&nbsp;
<i>27</i>&nbsp;/**
<i>28</i>&nbsp; * Uses the provided {@link AbstractPropertyAccessStrategy} to determine how to access an object´s properties.
<i>29</i>&nbsp; * &lt;p/&gt;
<i>30</i>&nbsp; * If a property should be accessed, this implementation performs a lookup of the appropriate strategy for
<i>31</i>&nbsp; * the requested property. Afterwards, another {@link PropertyAccessor} is used for accessing it.&lt;br/&gt;
<i>32</i>&nbsp; * If the property should be accessed via the getter and setter methods, a {@link BeanWrapperImpl} will be used.&lt;br/&gt;
<i>33</i>&nbsp; * Otherwise, a {@link DirectFieldAccessor} is used to access the properties directly without possible side-effects
<i>34</i>&nbsp; * caused by invoking getter or setter methods.
<i>35</i>&nbsp; *
<i>36</i>&nbsp; * @author Gregor Schauer
<i>37</i>&nbsp; * @since 2.0.0
<i>38</i>&nbsp; */
<i>39</i>&nbsp;public class StrategyPropertyAccessor extends AbstractPropertyAccessor {
<i>40</i>&nbsp;	protected AbstractPropertyAccessStrategy strategy;
<i>41</i>&nbsp;	protected ConfigurablePropertyAccessor fieldAccessor;
<i>42</i>&nbsp;	protected BeanWrapper propertyAccessor;
<i>43</i>&nbsp;	protected Object target;
<i>44</i>&nbsp;
<b class="fc"><i>45</i>&nbsp;	public StrategyPropertyAccessor(@Nonnull Object target, @Nonnull AbstractPropertyAccessStrategy strategy) {</b>
<b class="fc"><i>46</i>&nbsp;		this.target = target;</b>
<b class="fc"><i>47</i>&nbsp;		this.strategy = strategy;</b>
<b class="fc"><i>48</i>&nbsp;	}</b>
<i>49</i>&nbsp;
<i>50</i>&nbsp;	@Override
<i>51</i>&nbsp;	public boolean isReadableProperty(@Nonnull String propertyName) {
<b class="fc"><i>52</i>&nbsp;		return strategy.propertyAccessTypeMap.containsKey(propertyName);</b>
<i>53</i>&nbsp;	}
<i>54</i>&nbsp;
<i>55</i>&nbsp;	@Override
<i>56</i>&nbsp;	public boolean isWritableProperty(@Nonnull String propertyName) {
<b class="fc"><i>57</i>&nbsp;		return strategy.propertyAccessTypeMap.containsKey(propertyName);</b>
<i>58</i>&nbsp;	}
<i>59</i>&nbsp;
<i>60</i>&nbsp;	@Nonnull
<i>61</i>&nbsp;	@Override
<i>62</i>&nbsp;	public Class&lt;?&gt; getPropertyType(@Nonnull String propertyPath) {
<b class="fc"><i>63</i>&nbsp;		return getPropertyAccessor(propertyPath).getPropertyType(propertyPath);</b>
<i>64</i>&nbsp;	}
<i>65</i>&nbsp;
<i>66</i>&nbsp;	@Nonnull
<i>67</i>&nbsp;	@Override
<i>68</i>&nbsp;	public TypeDescriptor getPropertyTypeDescriptor(@Nonnull String propertyName) {
<b class="fc"><i>69</i>&nbsp;		return getPropertyAccessor(propertyName).getPropertyTypeDescriptor(propertyName);</b>
<i>70</i>&nbsp;	}
<i>71</i>&nbsp;
<i>72</i>&nbsp;	@Nullable
<i>73</i>&nbsp;	@Override
<i>74</i>&nbsp;	public Object getPropertyValue(@Nonnull String propertyName) {
<b class="fc"><i>75</i>&nbsp;		return getPropertyAccessor(propertyName).getPropertyValue(propertyName);</b>
<i>76</i>&nbsp;	}
<i>77</i>&nbsp;
<i>78</i>&nbsp;	@Override
<i>79</i>&nbsp;	public void setPropertyValue(@Nonnull String propertyName, @Nullable Object value) {
<b class="fc"><i>80</i>&nbsp;		getPropertyAccessor(propertyName).setPropertyValue(propertyName, value);</b>
<b class="fc"><i>81</i>&nbsp;	}</b>
<i>82</i>&nbsp;
<i>83</i>&nbsp;	@Override
<i>84</i>&nbsp;	public void setPropertyValue(@Nonnull PropertyValue pv) {
<b class="fc"><i>85</i>&nbsp;		getPropertyAccessor(pv.getName()).setPropertyValue(pv);</b>
<b class="fc"><i>86</i>&nbsp;	}</b>
<i>87</i>&nbsp;
<i>88</i>&nbsp;	/**
<i>89</i>&nbsp;	 * Returns the appropriate {@link PropertyAccessor} to use for accessing the given named property.&lt;br/&gt;
<i>90</i>&nbsp;	 * The decision is made by using the provided {@link AbstractPropertyAccessStrategy}.
<i>91</i>&nbsp;	 *
<i>92</i>&nbsp;	 * @param propertyName the name of the property to access
<i>93</i>&nbsp;	 * @return the property accessor to use
<i>94</i>&nbsp;	 * @see #getFieldAccessor()
<i>95</i>&nbsp;	 * @see #getPropertyAccessor()
<i>96</i>&nbsp;	 */
<i>97</i>&nbsp;	@Nonnull
<i>98</i>&nbsp;	public PropertyAccessor getPropertyAccessor(@Nonnull String propertyName) {
<b class="fc"><i>99</i>&nbsp;		AbstractPropertyAccessStrategy.AccessType accessType = strategy.getAccessType(propertyName);</b>
<b class="fc"><i>100</i>&nbsp;		return accessType == FIELD ? getFieldAccessor() : getPropertyAccessor();</b>
<i>101</i>&nbsp;	}
<i>102</i>&nbsp;
<i>103</i>&nbsp;	/**
<i>104</i>&nbsp;	 * Returns the {@link PropertyAccessor} to use for accessing the properties directly.&lt;br/&gt;
<i>105</i>&nbsp;	 * If the {@link PropertyAccessor} is not available, a new instance is created on-demand.
<i>106</i>&nbsp;	 *
<i>107</i>&nbsp;	 * @return the property accessor to use
<i>108</i>&nbsp;	 */
<i>109</i>&nbsp;	@Nonnull
<i>110</i>&nbsp;	public ConfigurablePropertyAccessor getFieldAccessor() {
<b class="fc"><i>111</i>&nbsp;		if (fieldAccessor == null) {</b>
<b class="fc"><i>112</i>&nbsp;			fieldAccessor = PropertyAccessorFactory.forDirectFieldAccess(target);</b>
<i>113</i>&nbsp;		}
<b class="fc"><i>114</i>&nbsp;		return fieldAccessor;</b>
<i>115</i>&nbsp;	}
<i>116</i>&nbsp;
<i>117</i>&nbsp;	/**
<i>118</i>&nbsp;	 * Returns the {@link PropertyAccessor} to use for accessing the properties via getter / setter methods.&lt;br/&gt;
<i>119</i>&nbsp;	 * If the {@link PropertyAccessor} is not available, a new instance is created on-demand.
<i>120</i>&nbsp;	 *
<i>121</i>&nbsp;	 * @return the property accessor to use
<i>122</i>&nbsp;	 */
<i>123</i>&nbsp;	@Nonnull
<i>124</i>&nbsp;	public BeanWrapper getPropertyAccessor() {
<b class="fc"><i>125</i>&nbsp;		if (propertyAccessor == null) {</b>
<b class="fc"><i>126</i>&nbsp;			propertyAccessor = PropertyAccessorFactory.forBeanPropertyAccess(target);</b>
<i>127</i>&nbsp;		}
<b class="fc"><i>128</i>&nbsp;		return propertyAccessor;</b>
<i>129</i>&nbsp;	}
<i>130</i>&nbsp;
<i>131</i>&nbsp;	/**
<i>132</i>&nbsp;	 * Casts the value to the required type.
<i>133</i>&nbsp;	 * &lt;p/&gt;
<i>134</i>&nbsp;	 * This method can be overridden to enable the {@link PropertyAccessor} to perform advanced property conversion
<i>135</i>&nbsp;	 * e.g., by using a {@link org.springframework.core.convert.ConversionService ConversionService}.
<i>136</i>&nbsp;	 *
<i>137</i>&nbsp;	 * @param value        the value to convert
<i>138</i>&nbsp;	 * @param requiredType the type to convert to (or {@code null} if not known, for example in case of a collection element)
<i>139</i>&nbsp;	 * @param methodParam  the method parameter that is the target of the conversion (for analysis of generic types; may be {@code null})
<i>140</i>&nbsp;	 * @return the new value, possibly the result of type conversion
<i>141</i>&nbsp;	 */
<i>142</i>&nbsp;	@Nullable
<i>143</i>&nbsp;	@Override
<i>144</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>145</i>&nbsp;	public &lt;T&gt; T convertIfNecessary(@Nullable Object value, @Nullable Class&lt;T&gt; requiredType, @Nullable MethodParameter methodParam) {
<b class="fc"><i>146</i>&nbsp;		return requiredType == null ? (T) value : requiredType.cast(value);</b>
<i>147</i>&nbsp;	}
<i>148</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2013-07-31 23:31</div>
</div>
</body>
</html>
